<?php
/**
 * CompleXml Framework
 *
 * @category   CompleXml
 * @package    CompleXml_Object
 * @copyright  Copyright (c) 2008 Andrey Kucherenko
 * @license    New BSD License
 * @version    $Id: Controller.php 9 2008-11-16 17:58:08Z kucherenko.andrey $
 */

require_once 'CompleXml/Config.php';
require_once 'CompleXml/View.php';
require_once 'CompleXml/Dataset/Request.php';
require_once 'CompleXml/Dataset/Cookies.php';
require_once 'CompleXml/Dataset/Session.php';
require_once 'CompleXml/Object/Router/Response.php';
require_once 'CompleXml/Object/Abstract.php';
class CompleXml_Object_Controller extends CompleXml_Object_Abstract
{
	/**
	 * CompleXml_Dataset_Request object
	 *
	 * @var CompleXml_Dataset_Request
	 */
	public $Request;
	/**
	 * CompleXml_Dataset_Session object
	 *
	 * @var CompleXml_Dataset_Session
	 */
	public $Session;
	/**
	 * CompleXml_Dataset_Cookies object
	 *
	 * @var CompleXml_Dataset_Cookies
	 */
	public $Cookies;
	/**
	 * CompleXml_Object_Router_Response object
	 *
	 * @var CompleXml_Object_Router_Response
	 */
	public $Response;
	/**
	 * CompleXml_View object
	 *
	 * @var CompleXml_View
	 */
	public $View;
	/**
	 * Current template
	 *
	 * @var string
	 */
	private $_template;
	/**
	 * Current locale
	 *
	 * @var string
	 */
	private $_locale;

	private $_session_name = '__CompleXml__Messages__';

	public function __construct ()
	{
		$this->Request = new CompleXml_Dataset_Request();

		$this->Session = new CompleXml_Dataset_Session();

		$this->Cookies = new CompleXml_Dataset_Cookies();

		$this->Response = new CompleXml_Object_Router_Response();

		$this->View = new CompleXml_View();

		$this->initHandlers();
		$this->initActions();
		$this->initView();

		$this->_restoreMessages();

	}

	protected function initView()
	{
		try{
			$template = CompleXml_Config::get('complexml_template');
			if (!is_null($template)){
				$this->View->setTemplate($template);
			}
		}catch(Exception $e){

		}

		try{
			$locale = CompleXml_Config::get('complexml_locale');
			if (!is_null($locale)){
				$this->View->setLocale($locale);
			}
		}catch (Exception $e){

		}
		$this->View->init();
	}

	protected function initActions()
	{
		try{
			$actions = (array) CompleXml_Config::get('complexml_actions');
			if (!empty($actions)){
				foreach ($actions as $action) {
					$this->addAction($action);
				}
			}
		}catch (Exception $e){

		}
	}

	protected function initHandlers()
	{
		try{
			$handlers = (array) CompleXml_Config::get('complexml_handlers');
			if (!empty($handlers)){
				foreach ($handlers as $handler) {
					$this->setHandler($handler);
				}
			}
		}catch (Exception $e){

		}
	}

	protected function _restoreMessages()
	{
		$_tmp_messages = $this->Session->getValue($this->_session_name);

		if (!is_null($_tmp_messages)){
			$this->Session->unregister($this->_session_name);
			$this->View->setMessages($_tmp_messages);
		}
	}

	protected function _goto ($url = null)
	{
		$messages = $this->View->getMessages();
		if (!empty($messages)){
			$this->Session->setValue($this->_session_name, $messages);
		}
		if (!is_null($url)){
			$this->Response->location($url);
		}else {
			$this->Response->rollback();
		}
	}

}